package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.MemberChannelDto;
import com.dingreading.cloud.soms.entity.MemberChannel;
import com.dingreading.cloud.soms.entity.table.MemberChannelTableDef;
import com.dingreading.cloud.soms.mapper.MemberChannelMapper;
import com.dingreading.cloud.soms.service.MemberChannelService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.soms.entity.table.StoreProductsTableDef.storeProducts;


/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class MemberChannelServiceImpl extends BaseServiceImpl<MemberChannelMapper, MemberChannel, MemberChannelTableDef> implements MemberChannelService {

    @Override
    protected MemberChannelTableDef getTable() {
        return MemberChannelTableDef.memberChannel;
    }

    @Override
    public Page<MemberChannelDto> pageList(PageUtil pageUtil, String corpUid, String storeUid, String keyword, Integer enabled, String queryStaffUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(queryStaffUid))
            condition.and(table.addStaffUid.eq(queryStaffUid));
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.fullName.like(keyword)
                    .or(table.name.like(keyword))
                    .or(table.mob.like(keyword)));
        }
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
//                .select(getDto())
//                .from(table)
                .leftJoin(storeProducts).on(table.productUid.eq(storeProducts.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, MemberChannelDto.class);
    }

//    private QueryColumn[] getDto() {
//        return new QueryColumn[]{table.corpUid, table.storeUid, table.storeName, table.name, table.productUid,
//                table.uid, table.enabled, table.fullName, table.mob, table.miniUrl, table.remark,
//                table.addTime, table.addStaffUid, table.addStaffName,
//                table.editTime, table.lastStaffUid, table.lastStaffName, table.refererStaffUid, table.refererStaffName,
//                table.refererStaffFullName, table.refererStaffPriPhone, table.refererStaffPubPhone, storeProducts.productName};
//    }

    @Override
    public MemberChannelDto getByStoreUidAndUid(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));

        QueryWrapper wrapper = QueryWrapper.create()
//                .select(getDto())
//                .from(table)
                .leftJoin(storeProducts).on(table.productUid.eq(storeProducts.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().selectOneByQueryAs(wrapper, MemberChannelDto.class);
    }

    @Override
    public MemberChannel getInfo(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean haveName(String storeUid, String uid, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.name.eq(name));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));

        return exists(condition);
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.uid.eq(uid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean updateEnabled(String storeUid, String uid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.uid.eq(uid));

        return UpdateChain.of(MemberChannel.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public boolean updateMiniUrl(String storeUid, String uid, String imgUrl, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.uid.eq(uid));

        return UpdateChain.of(MemberChannel.class)
                .set(table.miniUrl, imgUrl)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public boolean haveProductUid(String storeUid, String productUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        condition.and(table.productUid.eq(productUid));
        return exists(condition);
    }

    @Override
    public boolean haveChannelUid(String storeUid, String channelUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.uid.eq(channelUid));
        return exists(condition);
    }

    @Override
    public List<MemberChannelDto> listByEnabled(String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.name)
                .where(condition);

        return getMapper().selectListByQueryAs(wrapper, MemberChannelDto.class);
    }

    @Override
    public MemberChannel getByUid(String channelUid) {
        return getMapper().selectOneByCondition(table.uid.eq(channelUid));
    }
}
